package com.hmdp.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.BooleanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hmdp.dto.Result;
import com.hmdp.dto.UserDTO;
import com.hmdp.entity.Follow;
import com.hmdp.entity.User;
import com.hmdp.mapper.FollowMapper;
import com.hmdp.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hmdp.service.IUserService;
import com.hmdp.utils.UserHolder;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 小旭
 * @since 2021-12-22
 */
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {
     @Autowired
     private StringRedisTemplate stringRedisTemplate;

     @Autowired
     private IUserService userService;

    @Override
    public Result toFollowUser(Long id, Boolean isFollow) {
        //获取当前登录用户id
        Long userId = UserHolder.getUser().getId();
        String key = "follows:"+userId;
        //取消关注
        if (BooleanUtil.isFalse(isFollow)) {
            LambdaQueryWrapper<Follow> lqw = new LambdaQueryWrapper<>();
            lqw.eq(Follow::getUserId, userId);
            lqw.eq(Follow::getFollowUserId, id);
            this.remove(lqw);
            stringRedisTemplate.opsForSet().remove(key,id.toString());
        } else {
            //去关注
            Follow follow = new Follow();
            follow.setUserId(userId);
            follow.setFollowUserId(id);
            this.save(follow);
            stringRedisTemplate.opsForSet().add(key,id.toString());
        }
        return Result.ok();

    }

    @Override
    public Result isFollowed(Long id) {
        Long userId = UserHolder.getUser().getId();
        LambdaQueryWrapper<Follow> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Follow::getUserId, userId);
        lqw.eq(Follow::getFollowUserId, id);
        Follow user = this.getOne(lqw);
        if (user == null) {
            return Result.ok(false);
        } else {
            return Result.ok(true);
        }
    }

    @Override
    public Result commonFollow(Long id) {
        //获取登录用户id
        Long userId = UserHolder.getUser().getId();

        String key1 = "follows:"+userId;
        String key2 = "follows:"+id;
        Set<String> intersect = stringRedisTemplate.opsForSet().intersect(key1, key2);
        if(intersect.isEmpty()||intersect==null){
            return Result.ok(Collections.emptyList());
        }
        List<Long> idList = new ArrayList<>();
        for (String x: intersect) {
            long l = Long.parseLong(x);
            idList.add(l);
        }
        //将查询到的userList集合转化为userDtoList集合
        List<UserDTO> userDTOList = userService.listByIds(idList).
                stream()
                .map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());

        return Result.ok(userDTOList);
    }
}
